<?php

/**
 * 统计指标设置
 * 
 * @version 1.0
 * @author jinyue.wang
 * @date 2013-12-14
 */
class DefaultController extends FController {

    public $user_school_list;
    public $user_subject_list;
    public $user_role_list;
    public $exam_id;
    public function accessRules() {
        $config = array(
            'index' => 1
        );
        return ZRole::auth("indicators", $config);
    }

    public function __common() {
        parent::init();
        if (empty($this->cLoginUser)) {
            $this->redirect($this->createUrl('/site/login'));
        } else {
            $userLoginUser = $this->cLoginUser;
            $this->user_role_list = isset($userLoginUser['cuser']['role_id']) ? $userLoginUser['cuser']['role_id'] : '';
            $this->user_school_list = isset($userLoginUser['cuser']['school']['school_id']) ? $userLoginUser['cuser']['school']['school_id'] : '';
            $this->user_subject_list = isset($userLoginUser['cuser']['school']['subject_id']) ? $userLoginUser['cuser']['school']['subject_id'] : '';
            $zRow = new ZRole();
            //得到学校对应的考试
            $exam_list = $zRow->_getSchoolExam($this->user_school_list);
            if (!empty($exam_list)) {
                $this->exam_id = implode(',', $exam_list);
            }
        }

        $this->commonScript();
        Yii::app()->clientScript->registerCssFile(THEME_CSS_URL . '/module_Sip.css');
    }

    public function actionIndex() {
        $this->__common();
        Yii::app()->clientScript->registerScriptFile(STATICS_URL . "/js/jquery.zclip/jquery.zclip.js");

        $s['exam_id'] = $this->request->getParam('exam_id');
        
        $where = ' WHERE 1=1 ';
        if (empty($s['exam_id'])){
            //权限判断
            if ($this->user_role_list != 1 && !empty($this->exam_id)) {
                $where .= ' AND "exam_id" IN (' . $this->exam_id . ')';
            }
        }else{
            $where .= !empty($s['exam_id']) ? 'AND "exam_id" = '.$s['exam_id'] : '';
        }
        
        $order = ' ORDER BY "es_id" DESC ';
        
        $sqlSelect = 'SELECT "es_id","exam_id","subject_id","score_type1","score_type2","score_type3" FROM "{{subject_score}}" ';
        $sqlSelect .= $where.$order;
        
        $sqlCount = 'SELECT count(*) FROM "{{subject_score}}" ';
        $sqlCount .= $where.$order;
        
        $data = ZCommon::getDaoPageList($sqlSelect, $sqlCount, 8);

        $zExam = new zExam();
        $examArray = $zExam->_getExamObject($this->exam_id,$this->user_role_list);
        
        $zEdu = new ZEdu();
        $subjectObject = $zEdu->_getSubjectObject();
        
        $this->title = '(初中)统计指标设置';
        $pageUrl = Yii::app()->request->getUrl();
        $pageUrl = $this->createUrl("/indicators/default/index",$s);
        $this->render('index',array(
            'data'       => isset($data['listDatas'])?$data['listDatas']:'',
            'pages'      => isset($data['pages'])?$data['pages']:'',
            'pageUrl'    => $pageUrl,
            'examArray'  => $examArray,
            'subjectObject' => $subjectObject,
            'search'     => $s
        ));
    }

    public function actionUpdate(){
        
        $this->__common();
        $this->layout = "//layouts/main_window";
        
        $sid = $this->request->getParam('sid');
        
        $zExam = new zExam();
        if(isset($_POST) && !empty($_POST)){
            
            $data = $_POST;
        	
            $check = SubjectScore::model()->find(' "exam_id" = '.$data['exam_id'].' AND "subject_id" = '.$data['subject'].' AND "es_id" != '.$data['sid']);
            if(!empty($check)){
            	$this->frameAjaxMessage(null,'存在相同数据正在使用');
            	exit;
            }
            
           	$data['update_user'] = $this->cLoginUser['cuser']['id'];
           	$data['update_time'] = time();
            $status = $zExam->_updSubjectScore($_POST['sid'],$data);
            
            $this->frameAjaxMessage('/indicators/default/index');
            exit;
        }
        
        $zExam = new zExam();
        $examArray = $zExam->_getExamObject($this->exam_id,$this->user_role_list);
        
        $data = $zExam->_getSubjectScore($sid);
        
        $zEdu = new ZEdu();
        $subjectObject = $zEdu->_getSubjectObject();
        
        $this->render('update',array(
            'sid'   => $sid,
            'examArray' => $examArray,
            'subjectObject' => $subjectObject,
            'data'  => $data
        ));
    }
    
    public function actionAdd(){
        
        $this->__common();
        $this->layout = "//layouts/main_window";
        
        $zExam = new zExam();
        if(isset($_POST) && !empty($_POST)){
            
            $data = $_POST;
            $check = SubjectScore::model()->find(' "exam_id" = '.$data['exam_id'].' AND "subject_id" = '.$data['subject']);
            if(!empty($check)){
            	$this->frameAjaxMessage(null,'存在相同数据正在使用');
            	exit;
            }
            $data['create_user'] = $this->cLoginUser['cuser']['id'];
           	$data['create_time'] = time();
           	$data['update_user'] = $this->cLoginUser['cuser']['id'];
           	$data['update_time'] = time();
            $status = $zExam->_updSubjectScore(0,$data);
            
            $this->frameAjaxMessage('/indicators/default/index');
            exit;
        }
        
        $zExam = new zExam();
        $examArray = $zExam->_getExamObject($this->exam_id,$this->user_role_list);
        
        $this->render('add',array(
            'examArray' => $examArray
        ));
    }
    
    public function actionDelete(){
        
        $this->__common();
        
        $status = 0;
        $sid = $this->request->getParam('sid');
        if($sid>0){
            
            $sql = 'DELETE FROM "{{subject_score}}" WHERE "es_id" = :es_id';
            $result = Yii::app()->db->createCommand($sql);
            $result->bindParam(':es_id',$sid,PDO::PARAM_STR);
            
            $status = $result->execute();
        }
        echo $status;
    }
	/**
	* @ 批量删除学段
	* @ yindebin
	* @ 2014-01-06 pm
	*
	*/
	public function actionDelete_all(){
		$id = Yii::app ()->request->getparam ( 'id' );
		if (! empty ( $id )) {
			$delete = 'DELETE FROM "{{subject_score}}" WHERE "es_id" IN ('.$id.') AND "disabled" != 1';
			$query = Yii::app()->db->createCommand($delete)->execute();
			echo 1;
		}else{
			echo 2;
		}
	}
	
	public function actionCopy_data(){
		$conn =  Yii::app()->db; 
		$data = $_POST;
	
		$sql = 'SELECT * FROM "{{subject_score}}" WHERE "exam_id" = '.$data['old_exam_id'];
		$row = $conn->createCommand($sql)->queryAll();
		if(!empty($row)){
			
			$check = EduScore::model()->find('"exam_id" = '.$data['exam_id'].' AND "is_del" = 0');
			if(!empty($check)){
				$sql = 'DELETE FROM "{{subject_score}}" WHERE "exam_id" = :exam_id';
	            $result = Yii::app()->db->createCommand($sql);
	            $result->bindParam(':exam_id',$data['exam_id'],PDO::PARAM_STR);
	            $status = $result->execute();
			}
		
			foreach($row as $value){
				$insert = 'INSERT INTO "{{subject_score}}" ("exam_id","subject_id","score_type1","score_type2","score_type3","create_user","create_time","update_user","update_time")  VALUES('.$data['exam_id'].',\''.$value['subject_id'].'\',\''.$value['score_type1'].'\',\''.$value['score_type2'].'\',\''.$value['score_type3'].'\',\''.$this->cLoginUser['cuser']['id'].'\',\''.time().'\',\''.$this->cLoginUser['cuser']['id'].'\',\''.time().'\')';
				$result = $conn->createCommand($insert)->execute();
			}
		}else{
			$this->frameAjaxMessage(null,'要复制的数据不存在!');
			
		}
		$this->frameAjaxMessage('index');
		exit;
	}
	
	public function actionExam_select(){
		$this->layout = "//layouts/main_window";
		$exam_id = Yii::app()->request->getParam('exam_id');
		$exam = new ExamData();
		$exam_select = array();
		$exam_select = $exam->_getExam();
	
		$this->render('exam_select',array(
            'examArray' => $exam_select,
			'exam_id'	=> $exam_id
        ));
	}
	
	
	
	
}